package com.cee.admin.modules.prod.controller;

import cn.hutool.core.map.MapUtil;
import com.cee.admin.common.BaseController;
import com.cee.common.web.result.Result;
import com.cee.domain.modules.prod.vo.ProdProductDataReportVo;
import com.cee.ds.common.enums.ProductIndustry;
import com.cee.ds.common.utils.BasePrincipalUtils;
import com.cee.ds.modules.cfd.service.ICfdCarbonFootprintCertificateReportService;
import com.cee.ds.modules.cfd.service.ICfdCarbonNeutralityService;
import com.cee.ds.modules.cfd.service.ICfdCarbonPerformanceEvaluationService;
import com.cee.ds.modules.prod.service.IProdProductDataReportService;
import com.cee.ds.modules.prod.service.IProdProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 产品模块统计
 * <p>
 *
 * @author hys
 * @since 2024/4/19 16:32
 */
@Tag(name = "产品管理-整体统计")
@RestController
@RequestMapping("/api/prod/product/statistics")
public class ProdProductStatisticsController extends BaseController {

    @Autowired
    private IProdProductService iProdProductService;

    @Autowired
    private IProdProductDataReportService iProdProductDataReportService;

    @Autowired
    private ICfdCarbonNeutralityService iCfdCarbonNeutralityService;

    @Autowired
    private ICfdCarbonPerformanceEvaluationService iCarbonPerformanceEvaluationService;

    @Autowired
    private ICfdCarbonFootprintCertificateReportService iCarbonFootprintCertificateReportService;

    @Operation(summary = "数据统计")
    @GetMapping("/data/stats")
    @RequiresPermissions("prod:product:statistics")
    public Result<Map<String, Object>> dataStats() {
        Integer cityId = BasePrincipalUtils.currentUser().getCityId();
        Integer areaId = BasePrincipalUtils.currentUser().getAreaId();
        Map<String, Object> resultMap = MapUtil.<String, Object>builder()
                .put("productCount", iProdProductService.countWithCity(cityId, areaId))
                .put("neutralityCount", iCfdCarbonNeutralityService.countWithCity(cityId, areaId))
                .put("performanceCount", iCarbonPerformanceEvaluationService.countWithCity(cityId, areaId))
                .put("certificateCount", iCarbonFootprintCertificateReportService.countAcceptWithCity(cityId, areaId))
                .build();
        return success(resultMap);
    }

    @Operation(summary = "各行业产品总数占比分析")
    @GetMapping("/product/total/contrast")
    @RequiresPermissions("prod:product:statistics")
    public Result<Map<String, Long>> productTotalContrast() {
        Integer cityId = BasePrincipalUtils.currentUser().getCityId();
        Integer areaId = BasePrincipalUtils.currentUser().getAreaId();
        return success(iProdProductService.countWithCityGroupByIndustry(cityId, areaId));
    }

    @Operation(summary = "行业单位排放量排行")
    @GetMapping("/unit/emission/ranking")
    @RequiresPermissions("prod:product:statistics")
    public Result<Map<String, Double>> unitEmissionRanking() {
        Integer cityId = BasePrincipalUtils.currentUser().getCityId();
        Integer areaId = BasePrincipalUtils.currentUser().getAreaId();
        List<ProdProductDataReportVo> reportList = iProdProductDataReportService.unitEmissionRanking(null, cityId, areaId);
        Map<String, Double> collect = reportList.stream()
                .peek(item -> item.setIndustry(Objects.requireNonNull(ProductIndustry.getByCode(item.getIndustry())).getName()))
                .collect(Collectors.toMap(ProdProductDataReportVo::getIndustry, ProdProductDataReportVo::getUnitProductEmissions, (k1, k2) -> k1, LinkedHashMap::new));
        return success(collect);
    }

    @Operation(summary = "上年度综合产品类别单位产品排放量分析、上年度子行业单位产品排放量分析")
    @GetMapping("last/year/unit/emissions/analysis")
    @RequiresPermissions("prod:product:statistics")
    public Result<List<ProdProductDataReportVo>> lastYearUnitEmissionsAnalysis(@RequestParam String industry) {
        int lastYear = LocalDate.now().getYear() - 1;
        Integer cityId = BasePrincipalUtils.currentUser().getCityId();
        Integer areaId = BasePrincipalUtils.currentUser().getAreaId();
        return success(iProdProductDataReportService.lastYearUnitEmissionsAnalysisByProductType(industry, lastYear, null, cityId, areaId));
    }


}
